home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
cenvid
/
pi.cmm
< prev
next >
Wrap
Text File
|
1995-04-07
|
2KB
|
100 lines
/************************************************************************
* This is PI.CMM, (c) 1993 by LB (lbartho@uni2a.unige.ch) *
* permission is granted to Brent S. Noorda to use this code *
* Calling sequence: CEnvi PI [optional number of digits] *
************************************************************************/
#define DIGITSPERWORD 4 // 10^4 < sqrt(2^32)
#define BASE 10000
digits = 0; // default
words; // global
main( argc, argv )
{
if( argc == 2 )
digits = atoi(argv[1]);
if ( 2 < argc || digits < 1 )
Instructions();
words = ((digits + DIGITSPERWORD - 1)/ DIGITSPERWORD) + 1;
pi[0] = 2;
for( i = DIGITSPERWORD * words * 10 / 3, j = 2*i + 1; i >= 1; i--, j -= 2 )
{
if( i % 10 == 0 )
printf("[%d]\t\r", i );
muldiv( pi, i, j );
pi[0] += 2;
}
carryup( pi );
for( printf("%d.", pi[0] ), i = 1; i < words; print( pi[i++] ) );
printf("\n");
return 0;
}
muldiv( array, multiplicand, dividend )
{
carry = 0;
for( i = 0; i < words; i++ )
{
temp = array[i] * multiplicand + carry * BASE;
temp = ldiv( temp, dividend );
array[i] = temp.quot;
carry = temp.rem;
}
}
carryup( array )
{
for( i = words-1; i >= 0; i-- )
if( array[i] >= BASE )
{
array[i] = array[i] - BASE;
array[i-1]++;
}
}
col = 0;
print( n )
{
sprintf( format, "%%0%dd", DIGITSPERWORD );
sprintf( s, format, n );
for( i = 0; s[i] && digits--; i++ )
{
putchar( s[i] );
if( ++col % 5 == 0 )
{
putchar(' ');
if( col % 10 == 0 )
{
putchar(' ');
if( col == 50 )
{
col = 0;
printf("\n ");
}
}
}
}
}
Instructions()
{
puts("\a")
puts(`PI.CMM - Determine PI to arbitrary accuracy`)
puts(``)
puts(`USAGE: CEnvi PI [digits]`)
puts(``)
puts(`WHERE: digits - number of decimal digits to calculate; minimum 1`)
puts(``)
exit(EXIT_FAILURE);
}